import { getConfig } from '@/api/app'
import { defineStore } from 'pinia'
interface AppSate {
    config: Record<string, any>
    isMobile: boolean
    isCollapsed: boolean
    isRouteShow: boolean
}

const useAppStore = defineStore({
    id: 'app',
    state: (): AppSate => {
        return {
            config: {},
            isMobile: true,
            isCollapsed: false,
            isRouteShow: true
        }
    },
    actions: {
        // 对url过滤，如url是完整地址则直接返回；如不完整则拼接域名前缀
        getImageUrl(url: string) {
            return url && url.includes(this.config.oss_domain)
                ? url
                : `${this.config.oss_domain}${url}`
        },
        getConfig() {
            return new Promise((resolve, reject) => {
                this.config = {
                    "oss_domain": "https://likeshops.cyx.zzcyx1688.cn/",
                    "web_name": "Likeshop社区团购",
                    // "web_favicon": "https://likeshops.cyx.zzcyx1688.cn/uploads/images/20230301/202303011916399134f5553.png",
                    // "web_favicon": "@/assets/home/homeLog.png",
                    // "web_logo": "https://likeshops.cyx.zzcyx1688.cn/uploads/images/20230301/202303011927326d55e7165.png",
                    // "web_logo": "@/assets/home/homeLog.png",
                    "web_favicon": "@/assets/home/title.jpg",
                    "login_image": "https://likeshops.cyx.zzcyx1688.cn/uploads/images/20230301/202303011927597aa9c6929.png",
                    "copyright_config": [
                        // {
                        //     "key": "Copyright © 2023-2028 Likeshop",
                        //     "value": "https://beian.miit.gov.cn/#/Integrated/index"
                        // }
                    ],
                    "tencent_map_key": "WLVBZ-HXTCQ-CQ45I-2GIWK-VBREJ-UPFT3",
                    "document_status": 1,
                    "version": "1.8.0"
                }
                resolve(this.config)
                // getConfig()
                //     .then((data) => {
                //
                //     })
                //     .catch((err) => {
                //         reject(err)
                //     })
            })
        },
        setMobile(value: boolean) {
            this.isMobile = value
        },
        toggleCollapsed(toggle?: boolean) {
            this.isCollapsed = toggle ?? !this.isCollapsed
        },
        refreshView() {
            this.isRouteShow = false
            nextTick(() => {
                this.isRouteShow = true
            })
        }
    }
})

export default useAppStore
